

package com.example.administrator.caigou51.recyclerCard.cardView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.util.AbViewUtil;
import com.example.administrator.caigou51.EventBusClass.UpdateGoodsDetailViewPager;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.BigImageActivity;
import com.example.administrator.caigou51.activity.GoodsDetailsActivity;
import com.example.administrator.caigou51.activity.LoginActivity;
import com.example.administrator.caigou51.control.ImageCycleView;
import com.example.administrator.caigou51.control.ImageCycleViewGoods;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.recyclerCard.basic.CardItemView;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.GoodsDetailCard;
import com.example.administrator.caigou51.util.ImageUtil;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.widget.viewpagerindicator.CirclePageIndicator;


import java.util.ArrayList;

import de.greenrobot.event.EventBus;


public class GoodsDetailCardView extends CardItemView<GoodsDetailCard> implements View.OnClickListener{
 	private Context mContext;
    private MaterialListView materialListviewTaocangType;
    private MaterialListView materialListviewTaocangGoods;
    private MaterialListView materialListviewPingJia;
    private ViewPager WrapContentHeightViewPager;
    private LinearLayout linearLayoutZhiJiang;
    private ImageView imageViewGoodsZhiJiang;
    private ArrayList<Fragment> list;
    private CirclePageIndicator indicator;
    private TextView textViewSellScopen;
    private ImageCycleViewGoods mAdView;
    private ImageCycleViewGoods.ImageCycleViewListener mAdCycleViewListener;
    public static final String Tag_PuTong="0";
    public static final String Tag_ZhiJiang="1";
    public static final String Tag_ZheKou="2";
    public static final String Tag_ManJian="3";
    public GoodsDetailCardView(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public GoodsDetailCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public GoodsDetailCardView(Context context,
                               AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    private ImageView imageViewMore;
    private LinearLayout linearlayout_banner_container,linearLayoutGoToDetail,linearLayoutShare;
    private TextView textView_goods_price_lsj;
    private TextView textView_goods_price_pfj;
    private RelativeLayout relativeLayout_manjian;
    private TextView textView_chuxiao,textViewLine,textView_goods_yuan_pfj,textView_goods_zhi_jiang_tips,textViewKuCun;
    private TextView textView_manjian_extra;
    private RelativeLayout relativeLayout_zhijiang;
    private TextView textView_zhijiang,textViewChuXiaoDesc;
    private TextView textView_zhijiang_extra,textViewName,textViewAddress,textViewRmbICON;
    private GoodsDetailCard card;
    @Override
    public void build(final GoodsDetailCard card) {
        // TODO Auto-generated method stub
        super.build(card);
        this.card=card;
      //  GoodsPingJiaCard
      //  materialListviewPingJia
        //添加大图
        textViewKuCun= (TextView) findViewById(R.id.textViewKuCun);
        textViewSellScopen= (TextView) findViewById(R.id.textViewSellScopen);
        textView_goods_yuan_pfj = (TextView) findViewById(R.id.textView_goods_yuan_pfj);
        textView_goods_zhi_jiang_tips = (TextView) findViewById(R.id.textView_goods_zhi_jiang_tips);
        textViewName = (TextView) findViewById(R.id.textViewName);
        textViewAddress = (TextView) findViewById(R.id.textViewAddress);
        textView_goods_price_lsj = (TextView) findViewById(R.id.textView_goods_price_lsj);
        textView_goods_price_pfj = (TextView) findViewById(R.id.textView_goods_price_pfj);
        relativeLayout_manjian = (RelativeLayout) findViewById(R.id.relativeLayout_manjian);
        textView_chuxiao = (TextView) findViewById(R.id.textView_chuxiao);
        textView_manjian_extra = (TextView) findViewById(R.id.textView_manjian_extra);
        relativeLayout_zhijiang = (RelativeLayout) findViewById(R.id.relativeLayout_zhijiang);
        textView_zhijiang = (TextView) findViewById(R.id.textView_zhijiang);
        textView_zhijiang_extra = (TextView) findViewById(R.id.textView_zhijiang_extra);
        textViewRmbICON= (TextView) findViewById(R.id.textViewRmbICON);
        textViewLine= (TextView) findViewById(R.id.textViewLine);
        textView_goods_price_lsj.setText(card.getGoodsDetailBean().getPrice());
        textViewChuXiaoDesc= (TextView) findViewById(R.id.textViewChuXiaoDesc);
        textViewKuCun.setText("库存: "+card.getGoodsDetailBean().getAmount()+"");
        textViewName.setText(card.getGoodsDetailBean().getTitle()+card.getGoodsDetailBean().getProduct_spec());
        textViewAddress.setText(card.getGoodsDetailBean().getSubhead());

        linearLayoutShare= (LinearLayout) findViewById(R.id.linearLayoutShare);linearLayoutShare.setOnClickListener(this);
        linearLayoutZhiJiang= (LinearLayout) findViewById(R.id.linearLayoutZhiJiang);
        imageViewGoodsZhiJiang= (ImageView) findViewById(R.id.imageViewGoodsZhiJiang);

        //(1代表全国，2代表限广东）
        if ("2".equals(card.getGoodsDetailBean().getSale_range())) {
            textViewSellScopen.setText("销售范围:广东省");
        }else
        {
            textViewSellScopen.setText("销售范围:全国");
        }
        //判断促销信息
        if (Tag_PuTong.equals(card.getGoodsDetailBean().getPromotion()))
        {

            relativeLayout_zhijiang.setVisibility(View.GONE);
            textView_zhijiang_extra.setVisibility(View.GONE);
            textViewChuXiaoDesc.setText("批发价:");
            textView_goods_price_pfj.setText(card.getGoodsDetailBean().getShip_price());
            textView_goods_yuan_pfj.setVisibility(View.GONE);
            textView_goods_zhi_jiang_tips.setVisibility(View.GONE);
            linearLayoutZhiJiang.setVisibility(View.GONE);
        } else if (Tag_ZhiJiang.equals(card.getGoodsDetailBean().getPromotion()))
        {
            relativeLayout_zhijiang.setVisibility(View.VISIBLE);
            textView_manjian_extra.setVisibility(View.VISIBLE);
            textView_zhijiang.setText("直降");
            textViewChuXiaoDesc.setText("促销价:");
            textView_goods_price_pfj.setText(card.getGoodsDetailBean().getPromotion_price());
            textView_zhijiang_extra.setText(card.getGoodsDetailBean().getPromotion_name());
            textView_goods_yuan_pfj.setVisibility(View.VISIBLE);
            textView_goods_zhi_jiang_tips.setVisibility(View.VISIBLE);
            textView_goods_yuan_pfj.setText("￥"+card.getGoodsDetailBean().getShip_price());
            textView_goods_yuan_pfj.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
            textView_goods_zhi_jiang_tips.setText(card.getGoodsDetailBean().getPromotion_short_name());
            linearLayoutZhiJiang.setVisibility(View.VISIBLE);
            imageViewGoodsZhiJiang.setImageResource(R.drawable.icon_zj);
        }else if (Tag_ZheKou.equals(card.getGoodsDetailBean().getPromotion()))
        {
            relativeLayout_zhijiang.setVisibility(View.VISIBLE);
            textView_manjian_extra.setVisibility(View.VISIBLE);
            textView_zhijiang.setText("折扣");
            textViewChuXiaoDesc.setText("促销价:");
            textView_goods_price_pfj.setText(card.getGoodsDetailBean().getPromotion_price());
            textView_zhijiang_extra.setText(card.getGoodsDetailBean().getPromotion_name());
            textView_goods_yuan_pfj.setVisibility(View.VISIBLE);
            textView_goods_zhi_jiang_tips.setVisibility(View.VISIBLE);
            textView_goods_yuan_pfj.setText("￥"+card.getGoodsDetailBean().getShip_price());
            textView_goods_yuan_pfj.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
            textView_goods_zhi_jiang_tips.setText(card.getGoodsDetailBean().getPromotion_short_name());
            linearLayoutZhiJiang.setVisibility(View.VISIBLE);
            imageViewGoodsZhiJiang.setImageResource(R.drawable.icon_zk);
        }else
        {
            relativeLayout_zhijiang.setVisibility(View.GONE);
            textView_zhijiang_extra.setVisibility(View.GONE);
            textViewChuXiaoDesc.setText("批发价:");
            textView_goods_price_pfj.setText(card.getGoodsDetailBean().getShip_price());
            textView_goods_yuan_pfj.setVisibility(View.GONE);
            textView_goods_zhi_jiang_tips.setVisibility(View.GONE);
            linearLayoutZhiJiang.setVisibility(View.GONE);
        }
        //判断满减信息
        if (Tag_ManJian.equals(card.getGoodsDetailBean().getOrder_promotion()))
        {
            relativeLayout_manjian.setVisibility(View.VISIBLE);
            textView_manjian_extra.setVisibility(View.VISIBLE);
            textView_manjian_extra.setText(card.getGoodsDetailBean().getOrder_promotion_name());
        } else
        {
            relativeLayout_manjian.setVisibility(View.GONE);
            textView_manjian_extra.setVisibility(View.GONE);
        }
        //是否去掉灰线
        if (!Tag_PuTong.equals(card.getGoodsDetailBean().getPromotion())
                && !Tag_ZhiJiang.equals(card.getGoodsDetailBean().getPromotion())
                && !Tag_ZheKou.equals(card.getGoodsDetailBean().getPromotion())
                && !Tag_ManJian.equals(card.getGoodsDetailBean().getOrder_promotion())
                )
        {
            textViewLine.setVisibility(View.GONE);
        }else
        {
            textViewLine.setVisibility(View.VISIBLE);
        }

        //判断是否会员
        if (App.getUserBean()!=null && App.getUserBean().getIs_verified()==App.PassValue)
        {
            textViewRmbICON.setVisibility(View.VISIBLE);
        //    textView_goods_yuan_pfj.setVisibility(View.VISIBLE);
        //    textView_goods_zhi_jiang_tips.setVisibility(View.VISIBLE);
        }else
        {
            textViewRmbICON.setVisibility(View.GONE);
            textView_goods_yuan_pfj.setVisibility(View.GONE);
            textView_goods_zhi_jiang_tips.setVisibility(View.GONE);
            textView_goods_price_pfj.setText("会员可见");
        }

        linearlayout_banner_container = (LinearLayout) findViewById(R.id.linearlayout_banner_container);linearlayout_banner_container.setOnClickListener(this);
        linearLayoutGoToDetail= (LinearLayout) findViewById(R.id.linearLayoutGoToDetail);linearLayoutGoToDetail.setOnClickListener(this);

            mAdView = new ImageCycleViewGoods(mContext, 1, true);
            //设置广告栏长宽，与图片比例一致
        mAdView.setLayoutParams(new ViewGroup.LayoutParams(AbViewUtil.scaleValue(mContext,500), AbViewUtil.scaleValue(mContext,500)));
         //   mAdView.setLayoutParams(new ViewGroup.LayoutParams(App.getInstance().screenWidth, App.getInstance().screenWidth * 448 / 750));
        //   imageView.getLayoutParams().width=AbViewUtil.scaleValue(mContext,500);
        //    imageView.getLayoutParams().height=AbViewUtil.scaleValue(mContext,500);
            mAdView.setTag(AbViewUtil.NotScale);
            linearlayout_banner_container.addView(mAdView);
            linearlayout_banner_container.getLayoutParams().width=App.getInstance().screenWidth;
            linearlayout_banner_container.getLayoutParams().height=AbViewUtil.scaleValue(mContext,500);
        linearlayout_banner_container.setTag(AbViewUtil.NotScale);
            //广告栏监听事件
            mAdCycleViewListener = new ImageCycleViewGoods.ImageCycleViewListener() {
                //点击监听
                @Override
                public void onImageClick(String url, int position, View imageView) {
                    Intent intent=new Intent(mContext, BigImageActivity.class);
                    intent.putStringArrayListExtra(BigImageActivity.TagList, (ArrayList<String>) card.getGoodsDetailBean().getImages());
                    mContext.startActivity(intent);
                    //Intent intent = new Intent(mContext, FullImageActivity.class);
                    //mContext.startActivity(intent);
            /*        if (!TextUtils.isEmpty(info.getLink())) {
                        if (info.getLink().contains("http")) {
                            Intent intent = new Intent(mContext, WebActivityDiyUrl.class);
                            intent.putExtra(WebActivityDiyUrl.WebUrl, info.getLink());
                            intent.putExtra("title", info.getTitle());
                            mContext.startActivity(intent);
                        }else{
                            Intent intent = new Intent(mContext, ActivityActivity.class);
                            intent.putExtra("activity_id", info.getLink());
                            intent.putExtra("activity_name", info.getTitle());
                            mContext.startActivity(intent);
                        }
                    }*/
                }


                //加载数据
                @Override
                public void displayImage(String imageURL, ImageView imageView) {
//                    SimpleDraweeView simpleDraweeView = (SimpleDraweeView) imageView;
//                    GenericDraweeHierarchyBuilder builder =
//                            new GenericDraweeHierarchyBuilder(getResources());
//                    GenericDraweeHierarchy hierarchy = builder
//                            .build();
//                    hierarchy.setPlaceholderImage(R.drawable.default_img);
//                    simpleDraweeView.setHierarchy(hierarchy);
                    int aa = (int) Math.round(Math.random() * 10);
                  /*  if (aa%2==0)
                        imageView.setImageResource(R.drawable.test_banner_1);
                    else
                        imageView.setImageResource(R.drawable.test_banner_2);*/
                 //   imageView.getLayoutParams().width=AbViewUtil.scaleValue(mContext,500);
                //    imageView.getLayoutParams().height=AbViewUtil.scaleValue(mContext,500);
                    App.displayImageHttpOrFile(imageURL, imageView, ImageUtil.OptionsActivity());
                }
            };


            //设置广告栏内容
          /*  ArrayList<String> infoList =new ArrayList<>();
            infoList.add("11");
            infoList.add("121");*/
            mAdView.setImageResources(card.getGoodsDetailBean().getImages(), mAdCycleViewListener);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {

            case R.id.linearLayoutGoToDetail:
                EventBus.getDefault().post(new UpdateGoodsDetailViewPager(1));
               break;
          /*  case R.id.linearlayout_banner_container:
                Intent intent=new Intent(mContext, BigImageActivity.class);
                intent.putStringArrayListExtra(BigImageActivity.TagList, (ArrayList<String>) card.getGoodsDetailBean().getImages());
                mContext.startActivity(intent);
                break;*/
            case R.id.linearLayoutShare:
                if (App.getUserBean()!=null) {
                    App.showShareDialog(mContext, card);
                }else
                {
                    ToastUtil.showToast("请先登录,再分享");
                    Intent intent=new Intent(mContext,LoginActivity.class);
                    mContext.startActivity(intent);
                }
                break;

        }
    }








   /* public class GoodsRecommendAdapter extends PagerAdapter {
        public ArrayList<View> list;

        public GoodsRecommendAdapter(ArrayList<View> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return false;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //   LayoutInflater.from(mContext).inflate(R.layout.layout_goods_recommend, null);
            container.addView(list.get(position));
            return list.get(position);
           *//* ImageView imageView=new ImageView(getContext());
            imageView.setLayoutParams(new LayoutParams(700,700));
            imageView.setImageResource(R.drawable.test_3);*//*
            //  container.addView(imageView);
            //  return imageView;
        }
    }*/
        public class RecommendGoodsFragmentAdapter extends FragmentPagerAdapter {
            public ArrayList<Fragment> list;
            public RecommendGoodsFragmentAdapter(FragmentManager fm) {
                super(fm);
            }
            public RecommendGoodsFragmentAdapter(FragmentManager fm, ArrayList<Fragment> list){
                super(fm);
                this.list = list;
            }

            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }

    }





}
